package websocket.interceptor;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.springframework.http.server.ServerHttpRequest;  
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;

import org.springframework.web.socket.WebSocketHandler;  
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import websocket.WebSocketCenter;  
  
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{

    @Override  
    public boolean beforeHandshake(ServerHttpRequest request,  
            ServerHttpResponse response, WebSocketHandler wsHandler,  
            Map<String, Object> attributes) throws Exception {
   
    	if(request instanceof ServletServerHttpRequest){
//    		HttpServletRequest hsr = (HttpServletRequest)request;
    		WebSocketCenter.beforeHandshake(request, response, wsHandler, attributes);
    	}
        return super.beforeHandshake(request, response, wsHandler, attributes);  
    }  
  
    @Override  
    public void afterHandshake(ServerHttpRequest request,  
            ServerHttpResponse response, WebSocketHandler wsHandler,  
            Exception ex) {  
    	if (request instanceof ServletServerHttpRequest) {
    		ServletServerHttpRequest req = (ServletServerHttpRequest) request;
    		WebSocketCenter.afterHandshake(request, response, wsHandler, ex);
			System.out.println("After handshake with with client,headers:"+req.getHeaders());
			
		}
        super.afterHandshake(request, response, wsHandler, ex);  
    }
  
} 